<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 汪利东,2016-04-08 13:14
// +----------------------------------------------------------------------

namespace Admin\Controller;


use Admin\Common\Page;

class ActivityApplyController extends BaseController
{
    public function index()
    {
        $mapActivity=[
            'activity_end'=>['gt',date('Y-m-d H:i:s')]
        ];
        $activity = M('activity')->where($mapActivity)->select();
        $mapSrt = "activity_end > '".date('Y-m-d H:i:s')."'";

        $interval_time = I('get.interval_time');
        if (!empty($interval_time)) {
            $time=explode('~',$interval_time);
            $time[0]=$time[0].' 00:00:00';
            $time[1]=$time[1].' 23:59:59';

            $mapSrt .= "and activity_start > '$time[0]' and activity_start < '$time[1]'";
        }
        $in = M('Activity')->where($mapSrt)->fetchSql(false)->getField('activity_id',true);
        if(!empty($in)){
            $in = implode(",",$in);
            $map["a.activity_id"] = ["in",$in];
        }



        $activity_id = I('get.activity_id');
        if (!empty($activity_id)) {
            $where['a.activity_id'] = $activity_id;
            $where['_logic'] = 'AND';
            $map['_complex'] = $where;
        }

        $map['is_refund']='0';
        $count = M('ActivityApply')->alias('a')
            ->where($map)
            ->count();// 查询满足要求的总记录数
        $Page = new  Page($count, C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
//        $where['activity_id'] = $activity_id;
//        foreach ($where as $key => $val) {
//            $Page->parameter[$key] = urlencode($val);
//        }
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出

        $data = D('ActivityApply')->alias('a')
            ->join("left join card c on a.user_id=c.user_id")
            ->relation(true)
            ->where($map)
            ->order('activity_id desc')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->fetchSql(false)
            ->select();
//        var_dump($data);exit;

        $this->activity = $activity;


        $this->data = $data;
        $this->display();
    }

    function export(){
        $map=[
            'activity_end'=>['gt',date('Y-m-d H:i:s')]
        ];
        $activity = M('activity')->where($map)->select();

        $map = [];
        $activity_id = I('get.activity_id');
        if (!empty($activity_id)) {
            $map['a.activity_id'] = $activity_id;
//            $map['_logic'] = 'OR';
        }else{
            $a_id=[];
            foreach( $activity as $k=>$v){
                array_push($a_id,$v['activity_id']);
            }
            if(sizeof($a_id)>0){
                $map['a.activity_id']=['in',$a_id];
            }
        }
        $map['is_refund']='0';

        $data = D('ActivityApply')->alias('a')
            ->join("left join card c on a.user_id=c.user_id")
            ->where($map)
            ->order('activity_id desc')
            ->select();


        $file = excel_export($data);
        ouput_Browser($file);
    }
}